/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Nav} from '../../utils/nav.js';
var nav = new Nav();

Page({
    data: {
        pageIndex: 1,
        loadingHidden: false,
        isLoadedAll: false,
        dataArr: [],
        totalPage: 0,
        sid: null,
        img: null
    },
    onLoad: function(option) {
        var sid = option.sid;
        this.setData({
            sid: sid
        })
        switch(sid) {
            case '1': wx.setNavigationBarTitle({title:'生活小家电'});
                      this.setData({img:'../../imgs/s1.jpg'});
                      break;
            case '2': wx.setNavigationBarTitle({title:'母婴用品'});
                      this.setData({img:'../../imgs/s2.jpg'});
                      break;
            case '3': wx.setNavigationBarTitle({title:'水果生鲜'});
                      this.setData({img:'../../imgs/s3.jpg'});
        } 
        this._findSpecial(null, sid);
    },
    /**
     * 获取签到积分记录
     */
    _findSpecial: function(func) {
        func && func();
        var that = this,
            sid = that.data.sid;
        nav.findSpecial(sid, (data)=>{
            data.forEach((item) => {
                item.subTitle = Number(item.subTitle).toFixed(2);
            })
            that.setData({
                dataArr: data,
                loadingHidden: true
            })
        })
    },
    /**
     * 监听用户下拉动作 重新刷新用户收藏,回到&page=1
     */
    onPullDownRefresh: function() {
        var that = this;
        that.data.dataArr = [];  // 初始化
        that.data.pageIndex = 1;
        that._findSpecial(() => {
            that.setData({
                isLoadedAll: false
            });
            wx.stopPullDownRefresh()
        })
    },
    /**
     * 页面上拉触底事件的处理函数 上拉到底部触发&page=totalPage+1
     */
    onReachBottom: function() {
        if (!this.data.isLoadedAll && this.data.pageIndex < this.data.totalPage) {
            this.data.pageIndex++;
            this._findSpecial();
        }
        if (this.data.pageIndex == this.data.totalPage){
            this.setData({
                isLoadedAll: true
            })
        }
    }
})